package com.xuecheng.checkcode.controller;

import com.xuecheng.checkcode.model.CheckCodeParamsDto;
import com.xuecheng.checkcode.model.CheckCodeResultDto;
import com.xuecheng.checkcode.service.CheckCodeService;
import com.xuecheng.checkcode.service.SendCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 验证码服务接口
 */
@Api("验证码服务接口")
@RestController
public class CheckCodeController {
    @Resource(name = "PicCheckCodeService")
    private CheckCodeService picCheckCodeService;

    @Autowired
    SendCodeService sendCodeService;

    @ApiOperation(value="生成验证码信息",notes="生成验证码信息")
    @PostMapping("/pic")
    public CheckCodeResultDto generatePicCheckCode(CheckCodeParamsDto checkCodeParamsDto){
        return picCheckCodeService.generate(checkCodeParamsDto);
    }

    @ApiOperation(value="校验",notes="校验")
    @ApiImplicitParams({
        @ApiImplicitParam(name="name",value="业务名称",required=true,dataType="String",paramType="query"),
        @ApiImplicitParam(name = "key", value = "验证key", required = true, dataType = "String", paramType="query"),
        @ApiImplicitParam(name = "code", value = "验证码", required = true, dataType = "String", paramType="query")
    })
    @PostMapping("/verify")
    public Boolean verify(String key,String code){
        return picCheckCodeService.verify(key,code);
    }

    //这里传过来的名字是手机号的参数，但是发短信要钱，所以这里用qq邮箱，免费的，
    //前端页面中手机号那一栏直接填qq邮箱
    @ApiOperation("生成并发送验证码")
    @PostMapping("/phone")
    public CheckCodeResultDto generateCode(@RequestParam("param1")String email){
        //首先获取验证码
        CheckCodeParamsDto dto = new CheckCodeParamsDto();
        dto.setCheckCodeType("email");
        dto.setParam1(email);
        CheckCodeResultDto resultDto = picCheckCodeService.generateCode(dto);
        //然后向指定邮箱发送验证码
        sendCodeService.sendEmailCode(email,resultDto.getCode());
        return resultDto;
    }
}
